﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using EasyHook;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace IPokerBBMod
{

    public class Program : System.Windows.Forms.Form
    {
        private GroupBox Settings;
        private CheckBox startHook;
        private TextBox infoBox;
        private NotifyIcon notifyIcon1;
        private System.ComponentModel.IContainer components;
        private string clientName = string.Empty;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem closeToolStripMenuItem;
        private ToolStripMenuItem aboutToolStripMenuItem;
        static String ChannelName = null;

        Program()
        {
            InitializeComponent();
        }

        static void Main()
        {
            Application.EnableVisualStyles();

            Application.Run(new Program());
        }

        static int GetPid()
        {
            Process iPokerProcess = null;

            foreach (var proc in Process.GetProcesses())
            {
                if (proc.ProcessName == "casino")
                {
                    iPokerProcess = proc;
                    break;
                }
            }
            if (iPokerProcess == null)
            {
                MessageBox.Show("Poker client not detected. Please check that the client is running", "Oops! Something has gone wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return -1;
            }

            return iPokerProcess.Id;
        }




        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Program));
            this.Settings = new System.Windows.Forms.GroupBox();
            this.infoBox = new System.Windows.Forms.TextBox();
            this.startHook = new System.Windows.Forms.CheckBox();
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.closeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.Settings.SuspendLayout();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Settings
            // 
            this.Settings.Controls.Add(this.infoBox);
            this.Settings.Dock = System.Windows.Forms.DockStyle.Top;
            this.Settings.Location = new System.Drawing.Point(0, 0);
            this.Settings.Name = "Settings";
            this.Settings.Size = new System.Drawing.Size(298, 116);
            this.Settings.TabIndex = 0;
            this.Settings.TabStop = false;
            this.Settings.Text = "Info";
            // 
            // infoBox
            // 
            this.infoBox.BackColor = System.Drawing.Color.White;
            this.infoBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.infoBox.Location = new System.Drawing.Point(3, 16);
            this.infoBox.Multiline = true;
            this.infoBox.Name = "infoBox";
            this.infoBox.ReadOnly = true;
            this.infoBox.Size = new System.Drawing.Size(292, 97);
            this.infoBox.TabIndex = 1;
            // 
            // startHook
            // 
            this.startHook.Appearance = System.Windows.Forms.Appearance.Button;
            this.startHook.Font = new System.Drawing.Font("Segoe UI", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.startHook.Location = new System.Drawing.Point(87, 197);
            this.startHook.Name = "startHook";
            this.startHook.Size = new System.Drawing.Size(104, 55);
            this.startHook.TabIndex = 0;
            this.startHook.Text = "Start";
            this.startHook.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.startHook.UseVisualStyleBackColor = true;
            this.startHook.CheckedChanged += new System.EventHandler(this.startHook_CheckedChanged);
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.BalloonTipText = "IPoker BB Mod Status";
            this.notifyIcon1.BalloonTipTitle = "IPoker BB Mod";
            this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
            this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
            this.notifyIcon1.Visible = true;
            this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.closeToolStripMenuItem,
            this.aboutToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(104, 48);
            // 
            // closeToolStripMenuItem
            // 
            this.closeToolStripMenuItem.Name = "closeToolStripMenuItem";
            this.closeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.closeToolStripMenuItem.Text = "Close";
            this.closeToolStripMenuItem.Click += new System.EventHandler(this.closeToolStripMenuItem_Click);
            // 
            // aboutToolStripMenuItem
            // 
            this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            this.aboutToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.aboutToolStripMenuItem.Text = "About";
            this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
            // 
            // Program
            // 
            this.ClientSize = new System.Drawing.Size(298, 264);
            this.Controls.Add(this.Settings);
            this.Controls.Add(this.startHook);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "Program";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Tag = "";
            this.Text = "IPoker BB Mod";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Program_FormClosing);
            this.Resize += new System.EventHandler(this.Program_Resize);
            this.Settings.ResumeLayout(false);
            this.Settings.PerformLayout();
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        public class IPokerModInterface : MarshalByRefObject
        {
            public static bool x;
            public void ReportException(Exception InInfo)
            {
                
            }

            public void Ping()
            {
            }
        }

        private void startHook_CheckedChanged(object sender, EventArgs e)
        {
            var checkbox = sender as CheckBox;

            if (checkbox.Checked)
            {
                Int32 TargetPID = GetPid();
                Console.WriteLine("WTF");

                if (TargetPID == -1)
                {
                    infoBox.AppendText("Poker Client not detected\n");
                    checkbox.Checked = false;
                }

                else
                {
                    try
                    {
                        try
                        {
                            Config.Register(
                                "IPoker BB Modification",
                                "IPokerBBMod.exe",
                                "LunaInject.dll");
                        }
                        catch (ApplicationException)
                        {
                            MessageBox.Show("This is an administrative task!", "Permission denied...", MessageBoxButtons.OK);

                            System.Diagnostics.Process.GetCurrentProcess().Kill();
                        }

                        RemoteHooking.IpcCreateServer<IPokerModInterface>(ref ChannelName, WellKnownObjectMode.SingleCall);

                        RemoteHooking.Inject(
                            TargetPID,
                            "LunaInject.dll",
                            "LunaInject.dll",
                            ChannelName);

                        clientName = Regex.Match(Process.GetProcessById(TargetPID).MainModule.FileName, @".+(?=\\casino.exe)").Value;
                        clientName = clientName.Substring(clientName.LastIndexOf('\\'));
                        clientName = clientName.Replace("\\", "");

                        infoBox.AppendText(string.Format("Attached to Poker Client: {0}\n", clientName));
                        

                    }
                    catch (Exception ExtInfo)
                    {
                        Console.WriteLine("There was an error while connecting to target:\r\n{0}", ExtInfo.ToString());
                    }

                    checkbox.Text = "Exit";
                    checkbox.ForeColor = Color.Red;
                }
            }

            else if (!checkbox.Checked && checkbox.Text == "Exit")
            {
                Application.Exit();
            }

        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            notifyIcon1.Visible = true;
        }

        private void Program_FormClosing(object sender, FormClosingEventArgs e)
        {
            notifyIcon1.Visible = false;
        }

        private void Program_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                this.Hide();
                notifyIcon1.Visible = true;
                notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;

                if (this.startHook.Checked)
                {
                    notifyIcon1.BalloonTipText = string.Format("Mod is enabled on {0}", clientName);
                    notifyIcon1.ShowBalloonTip(1000);
                }
                else
                {
                    notifyIcon1.BalloonTipText = "Mod is not enabled";
                    notifyIcon1.ShowBalloonTip(1000);
                }
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form aboutBox = new AboutBox1();
            aboutBox.Show();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkbox = sender as CheckBox;
            IPokerModInterface.x = checkbox.Checked;
        }

    }
}